package com.datasecops.scannerservices.networksharing;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * @author: GeXiaoLong
 * @time: 2023/3/23 18:02
 */
public class NetworkSharedData {
     /**
      *网络共享数据落盘(文件)已弃用
      * @Description
      * @author GeXiaoLong
      * @time 2023/3/29 9:36
      * @param
      * @return
      */
    public void  sharedDataAcquisition(){
        // 共享文件夹路径
        String sharePath = "smb://10.26.18.4/incoming/测试数据/";
        //char[] chars = "123.4567a".toCharArray();
        try {
            // 设置登录信息
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "duser", "123.4567a");
            // 创建 SmbFile 对象
            SmbFile smbFile = new SmbFile(sharePath,auth);
            //smbFile.setAuthentication(auth);
            // 获取文件列表
            //String[] files = smbFile.list();
            SmbFile[] files = smbFile.listFiles();
            // 遍历文件列表
            for (SmbFile file : files) {
                if (file.isFile()) {
                    String fileName = file.getName();
                    long fileSize = file.length();
                    long lastModifiedTime = file.lastModified();
                    System.out.println("File Name: " + fileName + ", Size: " + fileSize + ", Last Modified Time: " + lastModifiedTime);
                    // 将文件内容写入到本地文件中
                    try (InputStream in = file.getInputStream();
                         OutputStream out = new FileOutputStream("P://text//souls//" + fileName)) {
                        byte[] buffer = new byte[8192];
                        int len;
                        while ((len = in.read(buffer)) != -1) {
                            out.write(buffer, 0, len);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println();
        }

    }

     /**
      *网络共享数据落盘(文件和文件夹)
      * @Description
      * @author GeXiaoLong
      * @time 2023/3/29 9:34
      * @param
      * @return
      */
    public void  sharedDataAcquisitions() {
        String sharePath = "smb://10.26.18.4/incoming/测试数据/";
        String localPath = "P://text//souls//";
/*
        String sharePath = "smb://username:password@server/share/path/";
        String localPath = "/path/to/local/directory/";
        */

        /*
    sharePath：网络共享路径，格式为 smb://username:password@server/share/path/，其中 username 和 password 分别是 SMB 认证的用户名和密码，server 是共享文件所在的服务器地址，share/path/ 是共享文件相对于共享根目录的路径。

    localPath：本地目录路径，指定下载后的文件和子目录要保存到本地的位置。

    auth：NtlmPasswordAuthentication 对象，用于进行 SMB 认证，其中 username 和 password 分别是 SMB 认证的用户名和密码。
    该代码会先判断共享路径是否是一个目录，如果是目录，会递归下载目录下的所有文件和子目录；如果是单个文件，则直接下载文件到本地目录。下载文件和子目录时，会使用 SMB 协议进行访问，通过 SMB 认证进行身份验证。

*/
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", "duser", "123.4567a");
        SmbFile smbFile;

        try {
            smbFile = new SmbFile(sharePath, auth);
            if (smbFile.isDirectory()) {
                // 获取目录下的文件和子目录
                SmbFile[] files = smbFile.listFiles();
                for (SmbFile file : files) {
                    String fileName = file.getName();
                    String filePath = localPath + fileName;
                    // 如果是子目录，递归下载子目录中的文件和子目录
                    if (file.isDirectory()) {
                        String utf8DirName = new String(file.getName().getBytes("GBK"), "UTF-8");
                        File localDir = new File(filePath + utf8DirName);
                        if (!localDir.exists()) {
                            localDir.mkdirs();
                        }
                        downloadDirectory(file, filePath, auth);
                    } else {
                        // 如果是文件，下载文件到本地目录
                        downloadFile(file, filePath, auth);
                    }
                }
            } else {
                // 如果是单个文件，下载文件到本地目录
                String fileName = smbFile.getName();
                String filePath = localPath + fileName;
                downloadFile(smbFile, filePath, auth);
            }
            System.out.println("下载完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 下载文件到本地目录
    private static void downloadFile(SmbFile smbFile, String localPath, NtlmPasswordAuthentication auth) {
        try (InputStream inputStream = smbFile.getInputStream();
             OutputStream outputStream = new FileOutputStream(localPath)) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 下载子目录中的文件和子目录
    private static void downloadDirectory(SmbFile smbDir, String localDir, NtlmPasswordAuthentication auth) {
        try {
            SmbFile[] files = smbDir.listFiles();
            for (SmbFile file : files) {
                String fileName = file.getName();
                String filePath = localDir + "/" + fileName;
                if (file.isDirectory()) {
                    String utf8DirName = new String(file.getName().getBytes("GBK"), "UTF-8");
                    File localSubDir = new File(filePath + utf8DirName);
                    if (!localSubDir.exists()) {
                        localSubDir.mkdirs();
                    }
                    downloadDirectory(file, filePath, auth);
                } else {
                    downloadFile(file, filePath, auth);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

//        File localDir = new File("P:\\text\\souls");
//        if (!localDir.exists()) {
//            localDir.mkdirs();
//        }
/*        String ss = "\\\\10.26.18.4\\incoming\\测试数据\\分类数据";
        System.out.println(ss);
        String[] parts = ss.split("\\\\");
        String ip = parts[2];
        System.out.println(ip);

        String replace = ss.replace("\\\\", "//").replace("\\", "/");
        System.out.println(replace);*/

        String path3 = "\\10.26.11.9\\incoming\\测试数据";
        String path4 = "\\10.26.11.9\\incoming\\测试数据\\FseClusterPackerNoAuth.exe";
        String newStr = path4.substring(path3.length() + 1);
        System.out.println(newStr);
        NetworkSharedData networkSharedData = new NetworkSharedData();
        networkSharedData.sharedDataAcquisitions();
    }
}
